package com.whut.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.*;
import com.whut.model.ContractCheckTask;
import com.whut.model.Contractinfo;
import com.whut.model.Contracttaskaccept;
import com.whut.model.Userinfo;
import com.whut.service.StatisticService;
import com.whut.utils.FormatDate;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.CheckAndProject;
import com.whut.v_model.ProblemNumSearch1;
import com.whut.v_model.TimesStat;
import com.whut.v_model.ProjectScore;
import com.whut.v_model.UnitRankStatistics;
import com.whut.v_model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
@Slf4j
@Service("statisticService")
public class StatisticServiceImpl implements StatisticService {
    @Resource
    private ContracttaskprojectMapper contracttaskprojectMapper;
    @Resource
    private ProblemcollectMapper problemcollectMapper;
    @Resource
    private ContracttaskacceptMapper contracttaskacceptMapper;
    @Resource
    private ProblemCollectDetailMapper problemCollectDetailMapper;
    @Resource
    private NewprefixsystemMapper newprefixsystemMapper;
    @Resource
    private ContractchecktaskMapper contractchecktaskMapper;
    @Resource
    private NewproblemdescriptionMapper newproblemdescriptionMapper;
    @Resource
    private AllpointsaveMapper allpointsaveMapper;
    @Resource
    private ResultsinglechildMapper resultsinglechildMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private ContractinfoMapper contractinfoMapper;

    @Resource
    private NewProblemCollectDetailMapper newproblemCollectDetailMapper;

    @Override
    public Object getUnitRanking(String startTime, String endTime, String unitType, int curPage, int pageSize) {
        List<Object> unitList1 = contracttaskprojectMapper.getAllUnitByUnitType(unitType);
        PageHelper.startPage(curPage, pageSize);
        List<Object> list = new ArrayList<Object>();
        List<Object> unitList = contracttaskprojectMapper.getAllUnitByUnitType(unitType);
        System.out.println(unitList.size());
        for (int i = 0; i < unitList.size(); i++) {
            int riskPoint0 = 0;
            int riskPoint1 = 0;
            int riskPoint2 = 0;
            int riskPoint3 = 0;
            int riskPoint4 = 0;
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("unitName", unitList.get(i).toString());
            List<Integer> projectIdList = contracttaskprojectMapper.getAllProjectId(unitType, "'" + unitList.get(i).toString() + "'");
            for (int j = 0; j < projectIdList.size(); j++) {
                riskPoint0 += problemcollectMapper.getCountRiskPointByType("提醒", projectIdList.get(j).intValue(), startTime, endTime);
                riskPoint1 += problemcollectMapper.getCountRiskPointByType("1", projectIdList.get(j).intValue(), startTime, endTime);
                riskPoint2 += problemcollectMapper.getCountRiskPointByType("2", projectIdList.get(j).intValue(), startTime, endTime);
                riskPoint3 += problemcollectMapper.getCountRiskPointByType("3", projectIdList.get(j).intValue(), startTime, endTime);
                riskPoint4 += problemcollectMapper.getCountRiskPointByType("4", projectIdList.get(j).intValue(), startTime, endTime);
            }
            dataMap.put("riskPointTX", riskPoint0);
            dataMap.put("riskPoint1", riskPoint1);
            dataMap.put("riskPoint2", riskPoint2);
            dataMap.put("riskPoint3", riskPoint3);
            dataMap.put("riskPoint4", riskPoint4);
            list.add(dataMap);
        }
        PageInfo result = new PageInfo(list);
        result.setTotal(unitList1.size());
        return result;
    }

    @Override
    public Object getAllUnitRanking(String startTime, String endTime, int curPage, int pageSize) {
        return null;
    }

    @Override
    public Object getTaskByContractId(String contractId) {
        JSONArray array = JSONArray.parseArray(contractId);
        List<Contracttaskaccept> contracttaskaccepts = new ArrayList<Contracttaskaccept>();
        for (int i = 0; i < array.size(); i++) {
            List<Contracttaskaccept> contracttaskacceptList = contracttaskacceptMapper.selectAllByContractId(array.getIntValue(i));
            contracttaskaccepts.addAll(contracttaskacceptList);
        }
        return contracttaskaccepts;
    }

    @Override
    public Object getProjectOnlyByTaskId(String taskId, String startTime, String endTime) {
        JSONArray jsonArray = JSONArray.parseArray(taskId);
        List<CheckAndProject> contracttaskprojects = new ArrayList<CheckAndProject>();
        for (int i = 0; i < jsonArray.size(); i++) {
            List<CheckAndProject> contracttaskprojectList = contracttaskprojectMapper.selectAllByTaskId(jsonArray.getIntValue(i), startTime, endTime);
            contracttaskprojects.addAll(contracttaskprojectList);
        }
        return contracttaskprojects;
    }

    @Override
    public Object getProjectByTaskId(String taskId, String startTime, String endTime, String unit) {
        JSONArray jsonArray = JSONArray.parseArray(taskId);
        JSONArray jsonArray1 = JSONArray.parseArray(unit);
        List<CheckAndProject> contracttaskprojects = new ArrayList<CheckAndProject>();
        for (int i = 0; i < jsonArray.size(); i++) {
            for (int j = 0; j < jsonArray1.size(); j++) {
                JSONArray jsonArray2 = JSONArray.parseArray(jsonArray1.getString(j));
                String unitType = jsonArray2.getString(0);
                String unitName = jsonArray2.getString(1);
                System.out.println(unitType + unitName);
                List<CheckAndProject> contracttaskprojectList =
                        contracttaskprojectMapper.selectAllByTaskIdAndTimeAndUnit(jsonArray.getIntValue(i), startTime, endTime, unitType, unitName);
                contracttaskprojects.addAll(contracttaskprojectList);
            }
        }
        return contracttaskprojects;
    }

    @Override
    public Object getAllUnitInfo(String contractId, String startTime, String endTime) {
        List<Object> list = new ArrayList<Object>();
        String unit[] = new String[3];
        String unitLabel[] = new String[3];
        unit[0] = "jsUnit";
        unit[1] = "jlUnit";
        unit[2] = "sgUnit";
        unitLabel[0] = "建设单位";
        unitLabel[1] = "监理单位";
        unitLabel[2] = "施工单位";
        JSONArray jsonArray = JSONArray.parseArray(contractId);
        for (int j = 0; j < unit.length; j++) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<Object> innerList = new ArrayList<Object>();
            dataMap.put("value", unit[j]);
            dataMap.put("label", unitLabel[j]);
            for (int i = 0; i < jsonArray.size(); i++) {
                List<String> inList = contracttaskprojectMapper.getUnitByTypeAndTime(jsonArray.getIntValue(i), unit[j]);
                for (int m = 0; m < inList.size(); m++) {
                    Map<String, Object> dataInnerMap = new HashMap<String, Object>();
                    dataInnerMap.put("label", inList.get(m));
                    dataInnerMap.put("value", inList.get(m));
                    innerList.add(dataInnerMap);
                }
                dataMap.put("children", innerList);
            }
            list.add(dataMap);
        }
        return list;
    }

    @Override
    public Object getProblemCollectInfo(String checkId) {
        List<Object> list = new ArrayList<Object>();
        List<Integer> projectList = new ArrayList<Integer>();
        System.out.println(checkId);
        if (checkId.indexOf("[") == -1) {
            projectList.add(Integer.parseInt(checkId));
        } else {
            JSONArray array = JSONArray.parseArray(checkId);
            for (int i = 0; i < array.size(); i++) {
                projectList.add(array.getIntValue(i));
            }
        }
        List<Integer> pcList1 = newproblemCollectDetailMapper.getAllFirstIdByProjectInfoList(projectList);
        for (int i = 0; i < pcList1.size(); i++) {
            List<Object> list1 = new ArrayList<Object>();
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("label", newprefixsystemMapper.getName4ByName4Id(pcList1.get(i)));
            map1.put("value", pcList1.get(i));
            List<Integer> pcList2 = newproblemCollectDetailMapper.getAllSecondIdByProjectInfoList(pcList1.get(i), projectList);
            for (int j = 0; j < pcList2.size(); j++) {
                List<Object> list2 = new ArrayList<Object>();
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("label", newprefixsystemMapper.getName4ByName4Id(pcList2.get(j)));
                System.out.println("第二级");
                map2.put("value", pcList2.get(j));
                List<Integer> pcList3 = newproblemCollectDetailMapper.getAllThirdIdByProjectInfoList(pcList2.get(j), projectList);
                for (int k = 0; k < pcList3.size(); k++) {
                    List<Object> list3 = new ArrayList<Object>();
                    Map<String, Object> map3 = new HashMap<String, Object>();
                    System.out.println("第三级");
                    map3.put("label", newprefixsystemMapper.getName4ByName4Id(pcList3.get(k)));
                    map3.put("value", pcList3.get(k));
                    List<Integer> pcList4 = newproblemCollectDetailMapper.getAllFourthIdByProjectInfoList(pcList3.get(k), projectList);
                    for (int l = 0; l < pcList4.size(); l++) {
                        List<Object> list4 = new ArrayList<Object>();
                        Map<String, Object> map4 = new HashMap<String, Object>();
                        System.out.println("第四级");
                        map4.put("label", newprefixsystemMapper.getName4ByName4Id(pcList4.get(l)));
                        map4.put("value", pcList4.get(l));
                        List<Integer> pcList5 = newproblemCollectDetailMapper.getAllFifthIdByProjectInfoList(pcList4.get(l), projectList);
                        for (int m = 0; m < pcList5.size(); m++) {
                            List<Object> list5 = new ArrayList<Object>();
                            Map<String, Object> map5 = new HashMap<String, Object>();
                            System.out.println("第五级");
                            map5.put("label", newprefixsystemMapper.getName4ByName4Id(pcList5.get(m)));
                            map5.put("value", pcList5.get(m));
                            List<Integer> pcList6 = newproblemCollectDetailMapper.getAllSixthIdByProjectInfoList(pcList5.get(m), projectList);
                            for (int n = 0; n < pcList6.size(); n++) {
                                Map<String, Object> map6 = new HashMap<String, Object>();
                                System.out.println("第六级");
                                map6.put("label", newprefixsystemMapper.getName4ByName4Id(pcList6.get(n)));
                                map6.put("value", pcList6.get(n));
                                list5.add(map6);
                            }
                            map5.put("children", list5);
                            list4.add(map5);
                        }
                        map4.put("children", list4);
                        list3.add(map4);
                    }
                    map3.put("children", list3);
                    list2.add(map3);
                }
                map2.put("children", list2);
                list1.add(map2);
            }
            map1.put("children", list1);
            list.add(map1);
        }
        return list;
    }

    @Override
    public Object getStatisticRiskPointInfo(String checkId, String system) {
        return null;
    }

    @Override
    public Object getProblemNumSearch1(String checkId, String system) {
        System.out.println("dsasdasdas" + checkId);
        List<Integer> checkList = new ArrayList<Integer>();
        if (!checkId.contains("[")) {
            checkList.add(Integer.valueOf(checkId));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(checkId);
            for (int i = 0; i < jsonArray.size(); i++) {
                checkList.add(jsonArray.getIntValue(i));
            }
        }
        //log.info("List:{}",checkList.size());
        JSONArray jsonArray1 = JSONArray.parseArray(system);
        int number = jsonArray1.getInteger(jsonArray1.size() - 1);
        List<ProblemNumSearch1> problemNumSearch1 = new ArrayList<ProblemNumSearch1>();
        String prenumName;
        String nextIdType;
        switch (jsonArray1.size()) {
            case 1:
                prenumName = "preNum2";
                nextIdType = "secondId";
                break;
            case 2:
                prenumName = "preNum3";
                nextIdType = "thirdId";
                break;
            case 3:
                prenumName = "preNum4";
                nextIdType = "fourthId";
                break;
            case 4:
                prenumName = "preNum5";
                nextIdType = "fifthId";
                break;
            case 5:
                prenumName = "preNum6";
                nextIdType = "sixthId";
                break;
            case 6:
                prenumName = "lastPreNum";
                nextIdType = "lastId";
                break;
            default:
                prenumName = "";
                nextIdType = "";
                break;
        }
        List<Integer> checkItemList = problemCollectDetailMapper.getAllNextIdByFather(checkList, number, prenumName);
        int allProblemNum = problemCollectDetailMapper.getAllProblemNumForStatistic(checkList, number, prenumName);
        for (int j = 0; j < checkItemList.size(); j++) {
            List<Integer> taskIdInfo = problemCollectDetailMapper.getAllTaskIdByCheckItemsAndCheckId(checkList, checkItemList.get(j), nextIdType);
            for (int k = 0; k < taskIdInfo.size(); k++) {
                ProblemNumSearch1 problemNumSearch11 = new ProblemNumSearch1();
                problemNumSearch11 = problemCollectDetailMapper.getProblemSearch1Info(checkList, taskIdInfo.get(k), checkItemList.get(j), nextIdType);
                int problemNum = problemCollectDetailMapper.getProblemNum(checkList, taskIdInfo.get(k), checkItemList.get(j), nextIdType);
                if (nextIdType.equals("lastId")) {
                    problemNumSearch11.setCheckItems(newproblemdescriptionMapper.getProblemDescriptionByKey(checkItemList.get(j)));
                } else {
                    problemNumSearch11.setCheckItems(newprefixsystemMapper.getName4ByName4Id(checkItemList.get(j)));
                }
                problemNumSearch11.setProblemNum(problemNum);
                int projectNum = contractchecktaskMapper.getProjectNum(checkList, taskIdInfo.get(k), checkItemList.get(j), nextIdType);
                problemNumSearch11.setProjectNum(projectNum);
                problemNumSearch11.setPercent(pointsCalculate.caculateToPercent(problemNum, allProblemNum));
                problemNumSearch1.add(problemNumSearch11);
            }
            System.out.println("hahahhaha" + allProblemNum);
        }
        return problemNumSearch1;
    }

    @Override
    public Object getRiskPointStatisticInfo(String checkId, String system) {
        List<Object> list = new ArrayList<Object>();
        List<Integer> checkList = new ArrayList<Integer>();
        if (!checkId.contains("[")) {
            checkList.add(Integer.valueOf(checkId));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(checkId);
            for (int i = 0; i < jsonArray.size(); i++) {
                checkList.add(jsonArray.getIntValue(i));
            }
        }
        JSONArray jsonArray1 = JSONArray.parseArray(system);
        int number = jsonArray1.getInteger(jsonArray1.size() - 1);
        String prenumName;
        String idType;
        switch (jsonArray1.size()) {
            case 1:
                prenumName = "preNum2";
                idType = "firstId";
                break;
            case 2:
                prenumName = "preNum3";
                idType = "secondId";
                break;
            case 3:
                prenumName = "preNum4";
                idType = "thirdId";
                break;
            case 4:
                prenumName = "preNum5";
                idType = "fourthId";
                break;
            case 5:
                prenumName = "preNum6";
                idType = "fifthId";
                break;
            case 6:
                prenumName = "lastPreNum";
                idType = "sixthId";
                break;
            default:
                prenumName = "";
                idType = "";
                break;
        }
        int allProblemNum = problemCollectDetailMapper.getAllProblemNumForStatistic(checkList, number, prenumName);
        TimesStat timesStat = problemcollectMapper.getRiskPointStaProNum(checkList, number, idType);
        for (int i = 1; i < 5; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            String riskLevelName;
            int problemNum;
            switch (i) {
                case 1:
                    riskLevelName = "特征值1和提醒（低隐患）";
                    problemNum = timesStat.getCount0() + timesStat.getCount1();
                    break;
                case 2:
                    riskLevelName = "特征值2（一般隐患）";
                    problemNum = timesStat.getCount2();
                    break;
                case 3:
                    riskLevelName = "特征值3（较大隐患）";
                    problemNum = timesStat.getCount3();
                    break;
                case 4:
                    riskLevelName = "特征值4（重大隐患）";
                    problemNum = timesStat.getCount4();
                    break;
                default:
                    riskLevelName = "";
                    problemNum = 0;
                    break;
            }

            map.put("riskLevelName", riskLevelName);
            map.put("problemNum", problemNum);
            map.put("percent", pointsCalculate.caculateToPercent(problemNum, allProblemNum));
            list.add(map);
        }
        return list;
    }

    /**
     * 根据委托方Id 与 搜索类型来搜索工程的统计信息
     *
     * @param contractId int 合同ID
     * @param type       int 搜索种类 0 搜索周期；1 月；2 季；3 年
     * @return List<ProjectScore>
     * @author fuzhenjie
     * @date 19.7.27 16:49
     */
    @Override
    public List<ProjectScore> getProjectScoreTendency(int contractId, int type) {
        if (type == 0) {
            //按照周期来查 即找出最近的两个周期，如果只找到一个，那就将第二个（稍早一些的）置-1
            List<Contracttaskaccept> list = contracttaskacceptMapper.getLatest2(contractId);
            int latestCTA1 = -1;
            //最新的周期 contractId
            int latestCTA2 = -1;
            //次新的周期
            if (list.size() == 2) {
                latestCTA1 = list.get(0).getTaskId();
                latestCTA2 = list.get(1).getTaskId();
            } else if (list.size() == 1) {
                latestCTA1 = list.get(0).getTaskId();
            }

            List<ProjectScore> listResult = contractchecktaskMapper.getLatestScoreCompare(latestCTA1, latestCTA2);
            for (ProjectScore item : listResult) {
                item.setTendency();
            }
            return listResult;
        }
        List<String> list = new ArrayList<>();
        /*switch (type){
            case 1:
                //月 则获取当月的日期范围和上月的日期范围
                list = FormatDate.getFormatRange(1);
                break;
            case 2:
                //季 则获取当季度的日期返回和上季度的日期范围
                list = FormatDate.getFormatRange(2);
                break;
            case 3:
                //年 则获取当年的日期范围和去年的日期范围
                list = FormatDate.getFormatRange(3);
                break;
            default:
        }*/
        list = FormatDate.getFormatRange(type);
        String st1, st2, et1, et2;
        st1 = list.get(0);
        et1 = list.get(1);
        st2 = list.get(2);
        et2 = list.get(3);
        List<ProjectScore> listResult = contractchecktaskMapper.getLatestTimeRangedScoreCompare(st1, et1, st2, et2, contractId);
        for (ProjectScore item : listResult) {
            item.setTendency();
        }
        return listResult;
    }

    @Override
    public Object getCheckFourthStatistics(int checkId) {
        return problemcollectMapper.getCheckFourthPoint(checkId);
    }

    @Override
    public List<UnitRankStatistics> getUnitPointRank(int contractId, int unitType, int timeType) {
        String unitName = "";
        //初始化 要选择的 参见单位类型 所对应的的列名
        switch (unitType) {
            case 0:
                unitName = "sgUnit";
                break;
            case 1:
                unitName = "jlUnit";
                break;
            case 2:
                unitName = "jsUnit";
                break;
            default:
        }
        List<String> list = FormatDate.getFormatRange(timeType);
        //得到两段时间的起始共四个参数
        String st1, st2, et1, et2;
        st1 = list.get(0);
        et1 = list.get(1);
        st2 = list.get(2);
        et2 = list.get(3);
        List<UnitRankStatistics> resultList = contractchecktaskMapper.getUnitPointRank(unitName, contractId, st1, et1, st2, et2);
        for (UnitRankStatistics item : resultList) {
            item.setTendency();
        }
        return resultList;
    }

    @Override
    public List<ContractCheckTask> getFinishedCheckByTaskId(int taskId, @Nullable String st, @Nullable String et) {
        return contractchecktaskMapper.getFinishedCheckByTaskId(taskId, st, et);
    }

    @Override
    public Object getProjectPiont(String checkId, int number) {
        JSONArray jsonArray = JSONArray.parseArray(checkId);
        List<Integer> list = jsonArray.toJavaList(Integer.class);

        return allpointsaveMapper.getProjectPiont(list, number);
        // return
    }

    /**
     * 为统计信息获取全部的人员信息
     *
     * @param ""
     * @return java.lang.Object
     * @author fanyu
     * @date 2019/8/5 17:17
     */
    @Override
    public Object getPersonInfoForSta() {
        List<Userinfo> userinfos=userinfoMapper.getAllUserInfo();
        return userinfos;
    }

    /*WYH*/
    @Override
    public Object unitProblemNumStatistic(String checkId, String unit, String system) {
        List<Object> unitNameList = new ArrayList<>();
        JSONArray unitJsonArray = JSONArray.parseArray(unit);
        JSONArray unit1JsonArray = JSONArray.parseArray(unitJsonArray.getString(0));
        String unitType = unit1JsonArray.getString(0);
        for(int i = 0; i < unitJsonArray.size(); i++){
            JSONArray unit2JsonArray = JSONArray.parseArray(unitJsonArray.getString(i));
            unitNameList.add(unit2JsonArray.getString(1));
        }
        String unitTypeName = "";
        if(unitType.equals("jsUnit"))
            unitTypeName = "建设单位";
        else if(unitType.equals("sgUnit"))
            unitTypeName = "施工单位";
        else if(unitType.equals("jlUnit"))
            unitTypeName = "监理单位";
        System.out.println("unitType:"+unitType);
        System.out.println("unitNameList:" + unitNameList);
        System.out.println("unitTypeName:" + unitTypeName);

        JSONArray checkIdArray = JSONArray.parseArray(checkId);
        List<Integer> checkIdList = new ArrayList<>();
        for(int i = 0; i < checkIdArray.size(); i++){
            checkIdList.add(checkIdArray.getIntValue(i));
        }

        System.out.println("systemLook:"+system);
        JSONArray systemJsonArray = JSONArray.parseArray(system);
        int preNum = systemJsonArray.getInteger(systemJsonArray.size() - 1);
        System.out.println("preNumLook：" + preNum);
        String prenumName;
        String pNameNum;
        String idType;
        switch (systemJsonArray.size()) {
            case 1:
                prenumName = "preNum2";
                idType = "firstId";
                pNameNum = "pName2";
                break;
            case 2:
                prenumName = "preNum3";
                idType = "secondId";
                pNameNum = "pName3";
                break;
            case 3:
                prenumName = "preNum4";
                idType = "thirdId";
                pNameNum = "pName4";
                break;
            case 4:
                prenumName = "preNum5";
                idType = "fourthId";
                pNameNum = "pName5";
                break;
            case 5:
                prenumName = "preNum6";
                idType = "fifthId";
                pNameNum = "pName6";
                break;
            case 6:
                prenumName = "lastPreNum";
                idType = "sixthId";
                pNameNum = "problemDescription";
                break;
            default:
                prenumName = "";
                idType = "";
                pNameNum = "";
                break;
        }
        System.out.println("prenumName:"+prenumName);
        System.out.println("idType:"+idType);
        System.out.println("pNameNum:"+pNameNum);

        List<UnitProblemNum> list = problemCollectDetailMapper.UnitProblemNumStatistic(checkIdList, unitNameList, unitType, idType, prenumName, pNameNum, preNum);

        int problemNum = 0;
        for(int i = 0; i < list.size(); i++){
            list.get(i).setUnitType(unitTypeName);
            problemNum += list.get(i).getProblemNum();
        }
        System.out.println("problemNum:" + problemNum);
        float precent = 0;
        for(int i = 0; i < list.size(); i++){
            list.get(i).setPercent(pointsCalculate.caculateToPercent(list.get(i).getProblemNum(),problemNum));
        }
        return list;
    }

    /**
     * @Author wenyihan
     * @Date 12:26 2019/8/9
     * @return
     */
    @Override
    public Object unitRiskPiontStatistic(String checkId, String unit){
        JSONArray checkIdArray = JSONArray.parseArray(checkId);
        List<Integer> checkIdList = new ArrayList<>();
        for(int i = 0 ; i < checkIdArray.size(); i++){
            checkIdList.add(checkIdArray.getIntValue(i));
        }

        JSONArray unitArray = JSONArray.parseArray(unit);
        JSONArray unit1JsonArray = JSONArray.parseArray(unitArray.getString(0));
        String unitType = unit1JsonArray.getString(0);
        List<Object> unitNameList = new ArrayList<>();
        for(int i = 0; i < unitArray.size(); i++){
            JSONArray unitNameArray2 = JSONArray.parseArray(unitArray.getString(i));
            unitNameList.add(unitNameArray2.getString(1));
        }
        String unitTypeName = "";
        if(unitType.equals("jsUnit"))
            unitTypeName = "建设单位";
        else if(unitType.equals("sgUnit"))
            unitTypeName = "施工单位";
        else if(unitType.equals("jlUnit"))
            unitTypeName = "监理单位";

        List<UnitRiskPoint> list = problemCollectDetailMapper.UnitRiskPointStatistic(checkIdList, unitNameList, unitType);
        for(int i = 0; i < list.size(); i++){
            list.get(i).setUnitType(unitTypeName);
        }
        return list;
    }

    @Override
    public Object getProjectForFuzzyCluster(String taskId, String startTime, String endTime,String clusterType) {
        JSONArray jsonArray = JSONArray.parseArray(taskId);
        System.out.println(clusterType);
        List<CheckAndProject> contracttaskprojects = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            List<CheckAndProject> contracttaskprojectList=new ArrayList<>();
            switch (clusterType)
            {
                case "1":
                    contracttaskprojectList = contracttaskprojectMapper.selectAllProjectForFuzzyClusterType1(jsonArray.getIntValue(i), startTime, endTime,clusterType);
                    break;
                case "2":
                    contracttaskprojectList = contracttaskprojectMapper.selectAllProjectForFuzzyClusterType2(jsonArray.getIntValue(i), startTime, endTime,clusterType);
                    break;
                case "3":
                    contracttaskprojectList = contracttaskprojectMapper.selectAllProjectForFuzzyClusterType3(jsonArray.getIntValue(i), startTime, endTime,clusterType);
                    break;
                    default:
            }
            contracttaskprojects.addAll(contracttaskprojectList);
        }
        return contracttaskprojects;
    }

    @Override
    public Object getClientInfoForUserStatistic(int userId, String startTime, String endTime) {
        List<Contractinfo> list=contractinfoMapper.getContractInfoForUserStatistic(userId,startTime,endTime);
        return list;
    }

    @Override
    public Object getTaskForUserStatistic(int userId, String startTime, String endTime, String contractId) {
        JSONArray jsonArray=JSONArray.parseArray(contractId);
        List<Contracttaskaccept> contracttaskacceptList=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++)
        {
            List<Contracttaskaccept> contracttaskacceptList1=contracttaskacceptMapper.getTaskForUserStatistic(userId,startTime,endTime,jsonArray.getIntValue(i));
            contracttaskacceptList.addAll(contracttaskacceptList1);
        }
        return contracttaskacceptList;
    }

    @Override
    public Object getProjectForUserStatistic(int userId, String startTime, String endTime, String taskId) {
        JSONArray jsonArray=JSONArray.parseArray(taskId);
        List<CheckAndProject> checkAndProjectList=new ArrayList<>();
        for(int i=0;i<jsonArray.size();i++)
        {
            List<CheckAndProject> checkAndProjectList1=contractchecktaskMapper.getProjectForUserStatistic(userId,startTime,endTime,jsonArray.getIntValue(i));
            checkAndProjectList.addAll(checkAndProjectList1);
        }
        return checkAndProjectList;
    }

    @Override
    public Object getSystemInfoForUserStatistic(int userId, String startTime, String endTime, String checkId) {
        List<Object> list = new ArrayList<Object>();
        List<Integer> projectList = new ArrayList<Integer>();
        System.out.println(checkId);
        if (checkId.indexOf("[") == -1) {
            projectList.add(Integer.parseInt(checkId));
        } else {
            JSONArray array = JSONArray.parseArray(checkId);
            for (int i = 0; i < array.size(); i++) {
                projectList.add(array.getIntValue(i));
            }
        }
        List<Integer> pcList1 = resultsinglechildMapper.getAllFirstIdByProjectInfoList(projectList,userId);
        for (int i = 0; i < pcList1.size(); i++) {
            List<Object> list1 = new ArrayList<Object>();
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("label", newprefixsystemMapper.getName4ByName4Id(pcList1.get(i)));
            map1.put("value", pcList1.get(i));
            List<Integer> pcList2 = resultsinglechildMapper.getAllSecondIdByProjectInfoList(pcList1.get(i), projectList,userId);
            for (int j = 0; j < pcList2.size(); j++) {
                List<Object> list2 = new ArrayList<Object>();
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("label", newprefixsystemMapper.getName4ByName4Id(pcList2.get(j)));
                System.out.println("第二级");
                map2.put("value", pcList2.get(j));
                List<Integer> pcList3 = resultsinglechildMapper.getAllThirdIdByProjectInfoList(pcList2.get(j), projectList,userId);
                for (int k = 0; k < pcList3.size(); k++) {
                    List<Object> list3 = new ArrayList<Object>();
                    Map<String, Object> map3 = new HashMap<String, Object>();
                    System.out.println("第三级");
                    map3.put("label", newprefixsystemMapper.getName4ByName4Id(pcList3.get(k)));
                    map3.put("value", pcList3.get(k));
                    List<Integer> pcList4 = resultsinglechildMapper.getAllFourthIdByProjectInfoList(pcList3.get(k), projectList,userId);
                    for (int l = 0; l < pcList4.size(); l++) {
                        List<Object> list4 = new ArrayList<Object>();
                        Map<String, Object> map4 = new HashMap<String, Object>();
                        System.out.println("第四级");
                        map4.put("label", newprefixsystemMapper.getName4ByName4Id(pcList4.get(l)));
                        map4.put("value", pcList4.get(l));
                        List<Integer> pcList5 = resultsinglechildMapper.getAllFifthIdByProjectInfoList(pcList4.get(l), projectList,userId);
                        for (int m = 0; m < pcList5.size(); m++) {
                            List<Object> list5 = new ArrayList<Object>();
                            Map<String, Object> map5 = new HashMap<String, Object>();
                            System.out.println("第五级");
                            map5.put("label", newprefixsystemMapper.getName4ByName4Id(pcList5.get(m)));
                            map5.put("value", pcList5.get(m));
                            List<Integer> pcList6 = resultsinglechildMapper.getAllSixthIdByProjectInfoList(pcList5.get(m), projectList,userId);
                            for (int n = 0; n < pcList6.size(); n++) {
                                Map<String, Object> map6 = new HashMap<String, Object>();
                                System.out.println("第六级");
                                map6.put("label", newprefixsystemMapper.getName4ByName4Id(pcList6.get(n)));
                                map6.put("value", pcList6.get(n));
                                list5.add(map6);
                            }
                            map5.put("children", list5);
                            list4.add(map5);
                        }
                        map4.put("children", list4);
                        list3.add(map4);
                    }
                    map3.put("children", list3);
                    list2.add(map3);
                }
                map2.put("children", list2);
                list1.add(map2);
            }
            map1.put("children", list1);
            list.add(map1);
        }
        return list;
    }

    @Override
    public Object getTableDataForUserStatistic(int userId, String checkInfo, String system) {
        List<Integer> checkList = new ArrayList<Integer>();
        if (!checkInfo.contains("[")) {
            checkList.add(Integer.valueOf(checkInfo));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(checkInfo);
            for (int i = 0; i < jsonArray.size(); i++) {
                checkList.add(jsonArray.getIntValue(i));
            }
        }
        JSONArray jsonArray1 = JSONArray.parseArray(system);
        int number = jsonArray1.getInteger(jsonArray1.size() - 1);
        List<ProblemNumSearch1> problemNumSearch1 = new ArrayList<ProblemNumSearch1>();
        String prenumName;
        String nextIdType;
        switch (jsonArray1.size()) {
            case 1:
                prenumName = "firstId";
                nextIdType = "secondId";
                break;
            case 2:
                prenumName = "secondId";
                nextIdType = "thirdId";
                break;
            case 3:
                prenumName = "thirdId";
                nextIdType = "fourthId";
                break;
            case 4:
                prenumName = "fourthId";
                nextIdType = "fifthId";
                break;
            case 5:
                prenumName = "fifthId";
                nextIdType = "sixthId";
                break;
            case 6:
                prenumName = "sixthId";
                nextIdType = "lastId";
                break;
            default:
                prenumName = "";
                nextIdType = "";
                break;
        }
        for(int g=0;g<checkList.size();g++)
        {
            List<Integer> checkItemList = resultsinglechildMapper.getAllNextIdByFather(checkList.get(g),number,prenumName,userId);
            int allProblemNum = resultsinglechildMapper.getAllProblemNumForStatistic(checkList.get(g), number, prenumName,userId);
            for (int j = 0; j < checkItemList.size(); j++) {
                List<Integer> taskIdInfo = resultsinglechildMapper.getAllTaskIdByCheckItemsAndCheckId(checkList.get(g), checkItemList.get(j), nextIdType,userId);
                for (int k = 0; k < taskIdInfo.size(); k++) {
                    ProblemNumSearch1 problemNumSearch11 = resultsinglechildMapper.getProblemSearch1Info(checkList.get(g), taskIdInfo.get(k), checkItemList.get(j), nextIdType);
                    int problemNum = resultsinglechildMapper.getProblemNum(checkList.get(g), taskIdInfo.get(k), checkItemList.get(j), nextIdType,userId);
                    if (nextIdType.equals("lastId")) {
                        problemNumSearch11.setCheckItems(newproblemdescriptionMapper.getProblemDescriptionByKey(checkItemList.get(j)));
                    } else {
                        problemNumSearch11.setCheckItems(newprefixsystemMapper.getName4ByName4Id(checkItemList.get(j)));
                    }
                    problemNumSearch11.setProblemNum(problemNum);
                    problemNumSearch11.setPercent(pointsCalculate.caculateToPercent(problemNum, allProblemNum));
                    problemNumSearch1.add(problemNumSearch11);
                }
            }
        }
        return problemNumSearch1;
    }

    @Override
    public Object getProjectByTask(String taskId,String startTime,String endTime) {
        List<Integer> list = new ArrayList<Integer>();
        if (!taskId.contains("[")) {
            list.add(Integer.valueOf(taskId));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(taskId);
            for (int i = 0; i < jsonArray.size(); i++) {
                list.add(jsonArray.getIntValue(i));
            }
        }
        return contractchecktaskMapper.getProjectByTask(list,startTime,endTime);
    }

    @Override
    public List<RiskWarning> getProblemByCheckandSystem(String checkId,String system){
        List<Integer> list = new ArrayList<Integer>();
        if (!checkId.contains("[")) {
            list.add(Integer.valueOf(checkId));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(checkId);
            for (int i = 0; i < jsonArray.size(); i++) {
                list.add(jsonArray.getIntValue(i));
            }
        }
        List<Integer> list1 = new ArrayList<Integer>();
        if (!system.contains("[")) {
            list1.add(Integer.valueOf(system));
        } else {
            JSONArray jsonArray1 = JSONArray.parseArray(system);
            for (int i = 0; i < jsonArray1.size(); i++) {
                list1.add(jsonArray1.getIntValue(i));
            }
        }
        if(list1.size()==1){
            Integer firstId=list1.get(0);
            return problemCollectDetailMapper.getProblemByCheckandFirstId(list,firstId);
        }
        if(list1.size()==2){
            Integer secondId=list1.get(1);
            return problemCollectDetailMapper.getProblemByCheckandSecondId(list,secondId);
        }
        if(list1.size()==3){
            Integer thirdId=list1.get(2);
            return problemCollectDetailMapper.getProblemByCheckandThirdId(list,thirdId);
        }
        if(list1.size()==4){
            Integer fourthId=list1.get(3);
            return problemCollectDetailMapper.getProblemByCheckandFourthId(list,fourthId);
        }
        if(list1.size()==5){
            Integer fifthId=list1.get(4);
            return problemCollectDetailMapper.getProblemByCheckandFifthId(list,fifthId);
        }
        if(list1.size()==6){
            Integer sixthId=list1.get(5);
            return problemCollectDetailMapper.getProblemByCheckandSixthId(list,sixthId);
        }
        return null;
    }

    @Override
    public Object getProblemsByContractandUnit(String contractId,String jsUnit ,String jlUnit,String sgUnit){
        List<Integer> contractlist = new ArrayList<Integer>();
        if (!contractId.contains("[")) {
            contractlist.add(Integer.valueOf(contractId));
        } else {
            JSONArray jsonArray = JSONArray.parseArray(contractId);
            for (int i = 0; i < jsonArray.size(); i++) {
                contractlist.add(jsonArray.getIntValue(i));
            }
        }

        JSONArray a = JSONArray.parseArray(jsUnit);
        List<String> jslist = new ArrayList<>();
        for (int i = 0; i < a.size(); i++) {
            jslist.add(a.getString(i));
        }
        JSONArray b = JSONArray.parseArray(jlUnit);
        List<String> jllist = new ArrayList<>();
        for (int i = 0; i < b.size(); i++) {
            jllist.add(b.getString(i));
        }
        JSONArray c = JSONArray.parseArray(sgUnit);
        List<String> sglist = new ArrayList<>();
        for (int i = 0; i < c.size(); i++) {
            sglist.add(c.getString(i));
        }
        List<RiskWarning> res =  newproblemCollectDetailMapper.getProblemsByContractandUnit(contractlist,jslist,jllist,sglist);
        List<PointAndProject> pt = contractchecktaskMapper.getFinishTimeForProject();
        for(int i=0;i<res.size();i++){
            for(int j=0;j<pt.size();j++){
                if(res.get(i).projectId.equals(pt.get(j).projectId)){
                    RiskWarning pp = res.get(i);
                    pp.setFinishTime(pt.get(j).getFinishTime());
                }
            }
        }
        return res;
    }

    @Override
    public Object getAllsgUnit(){
        return contracttaskprojectMapper.getAllsgUnit();
    }

    @Override
    public Object getAlljsUnit(){
        return contracttaskprojectMapper.getAlljsUnit();
    }

    @Override
    public Object getAlljlUnit(){
        return contracttaskprojectMapper.getAlljlUnit();
    }
}
